package com.extrace.map;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.extrace.loader.PackageRouteLoader;
import com.extrace.ui.main.ExTraceApplication;

public class LocationService extends Service {

    private static final String TAG = "LocationService";
    //声明AMapLocationClient类对象
    AMapLocationClient mLocationClient;
    // 声明AMapLocationClientOption对象
    public AMapLocationClientOption mLocationOption;
    private PackageRouteLoader mLoader;

    public LocationService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        getPosition();
    }

    public void getPosition() {
        //初始化定位
        mLocationClient = new AMapLocationClient(this.getApplicationContext());
        // 初始化AMapLocationClientOption对象
        mLocationOption = new AMapLocationClientOption();
        // 设置定位模式为AMapLocationMode.Hight_Accuracy，高精度模式。
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //设置定位间隔,单位毫秒,默认为20s
        mLocationOption.setInterval(20000);
        // 获取一次定位结果： //该方法默认为false。
        mLocationOption.setOnceLocation(false);
        mLocationOption.setOnceLocationLatest(false);
        //给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
        // 设置定位回调监听
        mLocationClient.setLocationListener(mLocationListener);
        // 启动定位
        mLocationClient.startLocation();

    }

    // 声明定位回调监听器
    public AMapLocationListener mLocationListener = new AMapLocationListener() {
        @Override
        public void onLocationChanged(AMapLocation amapLocation) {
            if (amapLocation == null) {
                Log.i(TAG, "amapLocation is null!");
                return;
            }
            if (amapLocation.getErrorCode() != 0) {
                Log.i(TAG, "amapLocation has exception errorCode:" + amapLocation.getErrorCode());
                return;
            }
            Double longitude = amapLocation.getLongitude();//获取经度
            Double latitude = amapLocation.getLatitude();//获取纬度
            SaveRoute(latitude, longitude);
            Log.i(TAG, "longitude:" + longitude + ",latitude：" + latitude);
            Log.i(TAG, "一次！！！！！！！！");
        }
    };

   public void SaveRoute(double x, double y) {
       if (((ExTraceApplication)getApplication()).getLoginUser().getURull() == 0)
           return;;
       Intent intent = new Intent();
       intent.putExtra("x", x);
       intent.putExtra("y", y);
       intent.setClass(this, SaveLocation.class);
       startActivity(intent);
    }

}
